//#include "stdafx.h"
#include "Carddrv.h"
#include "rfReader.h"

#define Para_Err			-5
/*******************************************************************************/

//rf_request Success:0 ERR:<0
int  rf_request(HANDLE icdev,unsigned char _Mode,unsigned __int16 *TagType)
{
	unsigned char cmd;
	unsigned char len;
	unsigned char data[2];
	int st;

	cmd=CMD_REQUEST;
	len=1;
	_Mode=_Mode&0x81;
	data[0]=_Mode;
	st=Send_Cmd(icdev,cmd,len,data);
	if(st!=0) return st;
	st = Receive_Cmd(icdev,(unsigned char *)TagType);
	return st;
}

//rf_anticoll Success:1 ERR:<0
int   rf_anticoll(HANDLE icdev,unsigned char _Bcnt,unsigned long *_Snr)
{
	unsigned char cmd;
	unsigned char len;
	unsigned char data[6];
	int st;
	
	cmd=CMD_ANTICOLL;
	len=5;
	data[0]=_Bcnt;
	*(unsigned long *)&data[1] = *_Snr;
	st=Send_Cmd(icdev,cmd,len,data);
	if(st!=0) return st;
	st=Receive_Cmd(icdev,(unsigned char *)_Snr);
	return st;
}

//rf_select Success:1 ERR:<0
int  rf_select(HANDLE icdev,unsigned long _Snr,unsigned char *_Size)
{
	unsigned char cmd;
	unsigned char len;
	unsigned char data[4];
	int st;
	cmd=CMD_SELECT;
	len=4;
	memcpy(data,&_Snr,4);
	st=Send_Cmd(icdev,cmd,len,data);
	if(st!=0) return st;
	return Receive_Cmd(icdev,_Size);
}

//Rf_Authentication Success:1 ERR:<0
int  rf_authentication(HANDLE icdev,unsigned char _Mode,unsigned char _SecNr)
{
	unsigned char cmd;
	unsigned char len;
	unsigned char data[2];
	int st;
	if(_Mode>0x07) return Para_Err;
	if(_SecNr>15) return Para_Err;
	cmd=CMD_AUTH;
	len=2;
    data[0]=_Mode;
    data[1]=_SecNr;
	st=Send_Cmd(icdev,cmd,len,data);
	if(st!=0) return st;
	return Receive_Cmd(icdev,data);
}

//Rf_Halt Success:1 ERR:<0
int  rf_halt(HANDLE icdev)
{
	unsigned char cmd;
	unsigned char len;
	unsigned char data[2];
	int st;
	cmd=CMD_HALT;
	len=0;
	st=Send_Cmd(icdev,cmd,len,data);
	if(st!=0) return st;
	return Receive_Cmd(icdev,data);
}

//Rf_Read
int  rf_read(HANDLE icdev,unsigned char _Adr,unsigned char *_Data)
{
	unsigned char cmd;
	unsigned char len;
	unsigned char data[2];
	int st;
	if(_Adr>63) return Para_Err;
	cmd=CMD_READ;
	len=1;
	data[0]=_Adr;
	st=Send_Cmd(icdev,cmd,len,data);
	if(st!=0) return st;
	return Receive_Cmd(icdev,_Data);
}                                   

//Rf_Read_hex
int  rf_read_hex(HANDLE icdev,unsigned char _Adr,char *_Data)
{
	unsigned char buffer[160];
	int st;

	st=rf_read(icdev,_Adr,buffer);
	if(st!=0)
		return st;
	hex_a(buffer,(unsigned char *)_Data,16);
	return st;
}                                   

int  rf_write(HANDLE icdev,unsigned char _Adr,unsigned char *_Data)
{
	unsigned char cmd;
	unsigned char len;
	unsigned char data[17];
	unsigned char buff[17];
//	unsigned char back[17];
	int st;
	cmd=CMD_WRITE;
	len=17;
	if(_Adr>63) return Para_Err;
	data[0]=_Adr;	
	memcpy(buff,_Data,16);
	memcpy(&data[1],_Data,16);
	st=Send_Cmd(icdev,cmd,len,data);
	if(st!=0) return st;
	st=Receive_Cmd(icdev,data);
//	if(st!=0) return st;
	//st=Rf_Read(icdev,_Adr,back);
	//if(st!=0) return st;
	//back[17]='\0';
//	buff[17]='\0';
    //if((strcmp((char *)buff,(char *)back))!=0) return -0x88;
	return st;
}
//Rf_Write_hex
int  rf_write_hex(HANDLE icdev,unsigned char _Adr,unsigned char *_Data)
{
	unsigned char temp[50];

	if(a_hex(_Data,temp,32)<0)
		return Para_Err;
	return rf_write(icdev,_Adr,temp);
}

//Rf_Load_Key
int  rf_load_key(HANDLE icdev,unsigned char _Mode,unsigned char _SecNr,unsigned char *_NKey)
{
	unsigned char cmd;
	unsigned char len;
	unsigned char data[256];
	int st;
	unsigned char tk[17][7]={
					{0xbd,0xde,0x6f,0x37,0x83,0x83},
					{0x14,0x8a,0xc5,0xe2,0x28,0x28},
					{0x7d,0x3e,0x9f,0x4f,0x95,0x95},
					{0xad,0xd6,0x6b,0x35,0xc8,0xc8},
					{0xdf,0xef,0x77,0xbb,0xe4,0xe4},
					{0x09,0x84,0x42,0x21,0xbc,0xbc},
					{0x5f,0xaf,0xd7,0xeb,0xa5,0xa5},
					{0x29,0x14,0x8a,0xc5,0x9f,0x9f},
					{0xfa,0xfd,0xfe,0x7f,0xff,0xff},
					{0x73,0x39,0x9c,0xce,0xbe,0xbe},
					{0xfc,0x7e,0xbf,0xdf,0xbf,0xbf},
					{0xcf,0xe7,0x73,0x39,0x51,0x51},
					{0xf7,0xfb,0x7d,0x3e,0x5a,0x5a},
					{0xf2,0x79,0x3c,0x1e,0x8d,0x8d},
					{0xcf,0xe7,0x73,0x39,0x45,0x45},
					{0xb7,0xdb,0x6d,0xb6,0x7d,0x7d}
				};
	cmd=CMD_LOAD_KEY;
	len=18;
	if(_Mode>0x07) return Para_Err;
	if(_SecNr>15) return Para_Err;
	data[0]=_Mode;
	data[1]=_SecNr;
	memcpy(&data[2],tk[_SecNr],6);
	memcpy(&data[10],_NKey,6);
	st=Send_Cmd(icdev,cmd,len,data);
	if(st!=0) return st;
	return Receive_Cmd(icdev,data);
}

int  rf_card(HANDLE icdev,unsigned char _Mode,unsigned long *_Snr)
{
	unsigned __int16 tt=0;
	unsigned long snr;
	unsigned char size;
	int st;
   	st=rf_request(icdev,_Mode,&tt);
	if(st!=0) return st;
	st=rf_anticoll(icdev,0x00,&snr);
	if(st!=0) return st;
	st=rf_select(icdev,snr,&size);
	if(st!=0) return st;
	memcpy(_Snr,&snr,4);
	return 0;
}
int  rf_card_2(HANDLE icdev,unsigned char _Mode,unsigned long *_Snr,unsigned __int16 *tt)
{
	unsigned long snr;
	unsigned char size;
	int st;
   	
	st=rf_request(icdev,_Mode,tt);
	if(st!=0) return st;
	st=rf_anticoll(icdev,0x00,&snr);
	if(st!=0) return st;
	st=rf_select(icdev,snr,&size);
	if(st!=0) return st;
	memcpy(_Snr,&snr,4);
	return 0;
}
int  Get_Version(HANDLE icdev,unsigned char *_Status)
{
	unsigned char cmd;
	unsigned char len;
	unsigned char data[1];
	int st;
	cmd=CMD_STATUS;
	len=0;
	data[0]=0x00;
	st=Send_Cmd(icdev,cmd,len,data);
	if(st!=0) return st;
	return Receive_Cmd(icdev,_Status);
}                                   
int  rf_beep(HANDLE icdev,unsigned short _Msec)
{
	unsigned char cmd;
	unsigned char len;
	unsigned char data[3];
	int st;
	unsigned char *any;
	cmd=CMD_BEEP;
	len = 2;
	data[0]=_Msec&0x00ff;
	data[1]=_Msec/256;	   
	st=Send_Cmd(icdev,cmd,len,data);
	if(st!=0) return st;
	any=NULL;
	delay(_Msec*10);
	st=Receive_Cmd(icdev,(unsigned char *)any);
	return st;
}

int  rf_disp(HANDLE icdev,__int16 len,unsigned char* digit)
{	
	unsigned char cmd;
	unsigned char data[8];
	int st;
	cmd=CMD_DISP;
	len = 8;
	st=Send_Cmd(icdev,cmd,(unsigned char)len,digit);
	if(st!=0) return st;
	st=Receive_Cmd(icdev,data);
	return st;
}
int  Read_Eeprom(HANDLE icdev,__int16 offset,__int16 lenth,unsigned char *receive_buffer)
{	
	unsigned char cmd;
	unsigned char len;
	unsigned char data[3];
	int st;
//	if((offset>=512)||(offset<0)) return Para_Err;	
	cmd=CMD_R_EEPROM;
//	if((lenth>32)||(lenth<=0))	return Para_Err;
	len=3;
	memcpy(&data[0],&offset,2);	
	data[2]=(unsigned char)lenth;
	st=Send_Cmd(icdev,cmd,len,data);
	if(st!=0) return st;
	return Receive_Cmd(icdev,receive_buffer);
}

//LOW level subroutine swr_eeprom
int  Write_Eeprom(HANDLE icdev,__int16 offset,__int16 lenth,unsigned char* snd_buffer)
{	
	unsigned char cmd;
	unsigned char len;
	unsigned char data[50];
	unsigned char any[50];
	int st;
//	if((offset>=512)||(offset<0)) return Para_Err;	
	cmd=CMD_W_EEPROM;
//	if((lenth>32)||(lenth<0))	return Para_Err;
	len=3+lenth;
	memcpy(&data[0],&offset,2);
	data[2]=(unsigned char)lenth;
	memcpy(&data[3],snd_buffer,lenth);
	st=Send_Cmd(icdev,cmd,len,data);
	if(st!=0) return st;
	return Receive_Cmd(icdev,any);
}

int  Get_Time(HANDLE icdev,unsigned char *receive_data)
{	
	unsigned char cmd;
	unsigned char len;
	unsigned char data[50];
	int st;
	cmd=CMD_GET_TIME;
	len=0;

	data[0]=0x00;
	st=Send_Cmd(icdev,cmd,len,data);
	if(st!=0) return st;
	return Receive_Cmd(icdev,receive_data);
}

int  Set_Time(HANDLE icdev,unsigned char *time)
{	
	unsigned char cmd;
	unsigned char len;
	unsigned char data[50],receive_data[50];
	int st;
	cmd=CMD_SET_TIME;
	len=7;

	memcpy(data,time,7);   //y.w.m.d.h.m.s
	st=Send_Cmd(icdev,cmd,len,data);
	if(st!=0) return st;
	return Receive_Cmd(icdev,receive_data);
}

int  Disp_Mode(HANDLE icdev,unsigned char mode)
{	
	unsigned char cmd;
	unsigned char len;
	unsigned char data[50];
	unsigned char any[50];
	int st;
	__int16 offset;

	offset=0x188;
	if((offset>=512)||(offset<0)) return Para_Err;	
	cmd=CMD_W_EEPROM;
	len=3+1;
	memcpy(&data[0],&offset,2);
	data[2]=1;
	data[3]=mode;
	st=Send_Cmd(icdev,cmd,len,data);
	if(st!=0) return st;
	return Receive_Cmd(icdev,any);
}

int  Contr_Mode(HANDLE icdev,unsigned char mode)
{	
	unsigned char cmd;
	unsigned char len;
	unsigned char data[50];
	unsigned char any[50];
	int st;
	__int16 offset;

	offset=0x18a;
//	if((offset>=512)||(offset<0)) return ERR_FORMAT;	
	cmd=CMD_W_EEPROM;
	len=3+1;
	memcpy(&data[0],&offset,2);
	data[2]=1;
	data[3]=mode;
	st=Send_Cmd(icdev,cmd,len,data);
	if(st!=0) return st;
	return Receive_Cmd(icdev,any);
}

int  Set_Bright(HANDLE icdev,unsigned char bright)
{	
	unsigned char cmd;
	unsigned char len;
	unsigned char data[50];
	unsigned char any[50];
	int st;
	__int16 offset;

	offset=0x189;
//	if((offset>=512)||(offset<0)) return ERR_FORMAT;	
	cmd=CMD_W_EEPROM;
	len=3+1;
	memcpy(&data[0],&offset,2);
	data[2]=1;
	data[3]=bright;
	st=Send_Cmd(icdev,cmd,len,data);
	if(st!=0) return st;
	return Receive_Cmd(icdev,any);
}
   
	

